package com.jsmile.cloud.accountcenter.account.repo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.accountcenter.account.dao.UserAddressDao;
import com.jsmile.cloud.accountcenter.account.model.UserAddress;
import com.jsmile.cloud.accountcenter.api.req.address.ReqAddressPageQuery;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:38:37
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class UserAddressSearchRepository extends ServiceImpl<UserAddressDao, UserAddress> {

    public IPage<UserAddress> pageQuery(ReqAddressPageQuery reqAddressPageQuery) {
        return this.baseMapper.selectPage(reqAddressPageQuery.getPage(), getQueryWrapper(reqAddressPageQuery));
    }

    private Wrapper getQueryWrapper(ReqAddressPageQuery reqAddressPageQuery) {
        return new QueryWrapper<UserAddress>().lambda().eq(UserAddress::getTenantId, reqAddressPageQuery.getCurTenantId())
            .eq(UserAddress::getUserId, reqAddressPageQuery.getCurUserId()).eq(null != reqAddressPageQuery.getShopId(), UserAddress::getShopId, reqAddressPageQuery.getShopId())
            .eq(null != reqAddressPageQuery.getAddressTypeEnum(), UserAddress::getAddressType, reqAddressPageQuery.getAddressTypeEnum().getValue())
            .orderByDesc(UserAddress::getCreateTime);// sort,createTime倒序
    }

}
